/**
 * Created by Luzanzan on 2016/10/9.
 */
angular.module('index',['base'])
.controller('indexcontroller',function($scope,baseservice,$sce,$location){
        $scope.qiniu="http://ob0l9nlbf.bkt.clouddn.com/";
        //等待加载
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $(".flexslider").flexslider({
                slideshowSpeed: 3000,
                pauseOnAction: true,
                after: function (slider) {
                    if (!slider.playing) {
                        slider.play();
                    }
                }
            });
        });
        //每日爆款车，轮播，广告
        baseservice.post('/advertisement/findAll')
            .success(function(data){
                console.log(data);
                $scope.baokuanfour=[];
                $scope.lunboList=[];
                $scope.maichead=[];
                $scope.yangchead=[];
                _.each(data,function(item){
                    if(item.platformtype=='PC'&&item.type==1){
                        $scope.baokuanfour.push(item);
                    }else if(item.platformtype=='PC'&&item.type==4){
                        $scope.lunboList.push(item);
                    }else if(item.platformtype=='PC'&&item.type==5){
                        $scope.maichead.push(item);
                    }else if(item.platformtype=='PC'&&item.type==6){
                        $scope.yangchead.push(item);
                    }
                })
                $scope.lunboList=_.sortBy($scope.lunboList,function(ite){return ite.seq;});
                $scope.showbaokuan=[];
                var i=0;
                _.each($scope.baokuanfour,function(item,idex){
                    if((idex+1)%2==1){              //从0开始,单
                        item.cishu=i;
                    }else {
                        item.cishu=i;
                        i=i+1;
                    }
                })
                $scope.showbaokuan= _.groupBy($scope.baokuanfour, function (i) {
                    return i.cishu
                });
                console.log($scope.showbaokuan);
                $scope.maicheshowad=$scope.maichead[0];
                $scope.yangcheshowad1=$scope.yangchead[0];
                $scope.yangcheshowad2=$scope.yangchead[1];
                $scope.yangcheshowad3=$scope.yangchead[2];
            })
        //首页买车列表
        baseservice.post('/carold/indextypepcfind',{
            page:1,row:6,order:'desc',sort:'createtime',indextypepc:'PPCHE'
        }).success(function(data){
            console.log(data);
            $scope.buycarList=data.rows;
            _.each($scope.buycarList,function (item) {
                item.picsurl=item.picsurl.split(",")[0];
            });
        })
        //买车颜色切换
        $scope.colormaiche=function($index){
            $("#buycar li").removeClass("active");
            $($("#buycar li").get($index)).addClass('active')
        }
        //切换买车列表
        $scope.showbuycar=function(type){
            baseservice.post('/carold/indextypepcfind',{
                page:1,row:6,order:'desc',sort:'createtime',indextypepc:type
            }).success(function(data){
                console.log(data);
                $scope.buycarList=data.rows;
                _.each($scope.buycarList,function (item) {
                    item.picsurl=item.picsurl.split(",")[0];
                });

            })
        }
        //首页养车列表
        baseservice.post('/beautiify/indexpushfind',{
            page:1,row:6,order:'desc',sort:'createtime'
        }).success(function(data){
            console.log(data);
            _.each(data.rows,function (e) {
                e.picurl=e.picurl.split(",");
                e.picurl=e.picurl[0];
            })
            $scope.yangcarList=data.rows;
            _.each($scope.yangcarList,function (item) {
                item.picurl=item.picurl.split(",")[0];
            });
        })
        //养车颜色切换
        $scope.coloryangche=function($index){
            $("#yangcar li").removeClass("active");
            $($("#yangcar li").get($index)).addClass('active')
        }
        $scope.showyangcar=function(type){
            if(type==0){
                baseservice.post('/beautiify/indexpushfind',{
                    page:1,row:6,order:'desc',sort:'createtime'
                }).success(function(data){
                    console.log(data);
                    $scope.yangcarList=data.rows;
                    _.each($scope.yangcarList,function (item) {
                        item.picurl=item.picurl.split(",")[0];
                    });
                })
            }else if(type==1){
                baseservice.post('/auxiliary/indexpushfind',{
                    page:1,row:6,order:'desc',sort:'createtime'
                }).success(function(data){
                    console.log(data);
                    $scope.yangcarList=data.rows;
                    _.each($scope.yangcarList,function (item) {
                        item.picurl=item.picurl.split(",")[0];
                    });
                })
            }else{
                $scope.yangcarList='';
            }
        }
        //获取车的详情
        baseservice.post('/carModel/type/findAll')
            .success(function (data) {
                $scope.showbrand=[];
                console.log(data);
                _.each(data,function(cc){
                    if(cc.shelvestype==true){
                        $scope.showbrand.push(cc);
                    }
                })
                $scope.brand=_.groupBy($scope.showbrand,function(i){
                    return  i.brand;
                });
                $scope.brandlist= _.keys($scope.brand);
                $scope.chexing=_.groupBy($scope.showbrand,function(i){
                    return  i.cheixing;
                });
                $scope.chexinglist= _.keys($scope.chexing);
            })
        //主页搜索
        $scope.searchkey;
        //默认
        $scope.moren=function(){
            if($scope.searchkey==null){
                $scope.searchkey='兰博基尼';
            }
        }
        $scope.searchindex=function(){
            console.log($scope.searchkey);
            window.location.href="search_page.html?key="+$scope.searchkey;
        }
        //主页活动分享展示
$scope.act={};

    $scope.indexact=function () {
        $scope.act.but1="actives";
        $scope.act.but2="";
        baseservice.post("/activity/findAll",{
            page:1,
            row:9999,
            order:"asc",
            sort:"createtime"

        }).success(function (data) {
            data=_.filter(data.rows,function (e) {
                return e.qiyoushare==false;
            })
            data=_.filter(data,function (e) {
                return e.status==2;
            })
console.log(data)
            $scope.act.first="";
            $scope.act.second=""
            $scope.act.first=data[0];
            $scope.act.second=data[1];
            console.log($scope.act.first);
            console.log($scope.act.second);
        }).error(function (data) {

        })
    }
    $scope.indexfx=function () {
        $scope.act.but1="";
        $scope.act.but2="actives";
        baseservice.post("/activity/findAll",{
            page:1,
            row:9999,
            order:"asc",
            sort:"createtime"

        }).success(function (data) {
            data=_.filter(data.rows,function (e) {
                return e.qiyoushare==true;
            })
            data=_.filter(data,function (e) {
                return e.status==2;
            })
            $scope.act.first="";
            $scope.act.second="";
            $scope.act.first=data[0];
            $scope.act.second=data[1];
            console.log($scope.act.first);
            console.log($scope.act.second);
        }).error(function (data) {

        })
    }
    $scope.openact=function (id,type) {
        if(type==true){
               window.location.href="template/active/news_detail.html?id="+id;
        }else if(type==false){
            // baseservice.poploginifneccisary(function(){
            window.location.href="template/active/activity_detail.html?id="+id;
            // })
        }

    }
    $scope.openfx=function (id) {
        window.location.href = "template/active/new_detail.html?id=" + id;
    }
        //轮播广告
        $scope.openlunbodetail=function (id) {
                window.open(id);
        }
        //点击去买车的页面品牌，价格，车型
        $scope.gobuycarbrand=function(brand){
            window.location.href="template/products/products_list_new.html?brand="+brand;
        }
        $scope.gobuycarprice=function(pricet,pricel){
            window.location.href="template/products/products_list_new.html?pricet="+pricet+"&&pricel="+pricel;
        }
        $scope.gobuycarchexing=function(chexing){
            window.location.href="template/products/products_list_new.html?chexing="+chexing;
        }
        //爆款车链接
        $scope.opennewcardetail=function (id) {
            window.open("template/products/products_detail_new.html?id="+id);
        }
        $scope.openfj=function (id) {
            if (id.substr(id.length-3) == "A03") {
                window.open("template/products/products_detail_fitting.html?id=" + id);;
            }else if(id.substr(id.length-3) == "B04"){
                window.open("template/products/products_detail_beauty.html?id="+id);
            }
        }
        //底部
        $scope.about_we={};
        $scope.findalll=function () {
            $scope.geturl=$location.absUrl();
            var id=decodeURI($scope.geturl.substr($scope.geturl.indexOf("id=")+3));
            var ty=decodeURI($scope.geturl.substr($scope.geturl.indexOf("type=")+5));
            var long=id.length-ty.length-6;
            id=id.substring(0,long);
            $scope.ztchange=ty;
            baseservice.post('/staticpage/findAll')
                .success(function (data) {
                    $scope.about_welist=data;
                    for(var i=0;i<data.length;i++){
                        if(data[i].id==id){
                            $scope.about_welist[i].ynshow=true;
                        }
                        else {
                            $scope.about_welist[i].ynshow=false;
                        }
                    }

                });
            baseservice.post('/staticpage/findone',
                {
                    staticpageid:id
                })
                .success(function (data) {

                    $scope.about_we.txt=$sce.trustAsHtml(data.descripton);
                })
        }

    })